package com.sharding.sphere.demo.algorithm;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.hint.HintShardingValue;

import java.util.Arrays;
import java.util.Collection;

/**
 * @author: shuguang.zhang
 * @date : 2021-06-04 17:55
 */
@Slf4j
public class TradeOrderHintDBShardingStrategy implements HintShardingAlgorithm<Long> {

    @Override
    public Collection<String> doSharding(Collection<String> databaseNames, HintShardingValue<Long> hintShardingValue) {
        log.info("TradeOrderHintDBShardingStrategy---> databaseNames:[{}] hintShardingValue:[{}]",databaseNames, hintShardingValue );

        String key = "m".concat(hintShardingValue.getValues().toArray()[0].toString());
        if(databaseNames.contains(key)){
            return Arrays.asList(key);
        }

        throw new UnsupportedOperationException();
    }
}
